import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import numeral from 'numeral';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import * as menuService from '../../../services/menus';
import * as common from '../../../utils/common';
import * as accountService from '../../../services/AccountDivi';

const daoName = "VIndiClie";
const ButtonGroup = Button.Group;
const columns = [
  { title: '证件号码', width: 150, sorter: true, dataIndex: 'IDNO', key: 'IDNO', dispaly: true, filter: true },
  { title: '客户名称', width: 120, sorter: true, dataIndex: 'INCN', key: 'INCN', dispaly: true, filter: true },
  { title: '认定时间', width: 110, sorter: true, key: 'RDSJ', caption: "RDSJ", dispaly: true, filter: true, render: data => datetime.format(data.RDSJ, "yyyy-MM-dd") },
  { title: '电话', width: 110, sorter: true, dataIndex: 'TLNO', key: 'TLNO', dispaly: true, filter: true },
  { title: '地址', width: 300, sorter: true, dataIndex: 'ADDR', key: 'ADDR', dispaly: true, filter: true },
  { title: '邮编', width: 110, sorter: true, dataIndex: 'POCD', key: 'POCD', dispaly: true, filter: true },
  { title: '电子邮箱', width: 180, sorter: true, dataIndex: 'EMAL', key: 'EMAL', dispaly: false, filter: true },
];
const controls = [
  { name: 'IDNO', required: true, type: 'text', label: '证件号码', grid: 12 / 24 },
  { name: 'INCN', required: true, type: 'text', label: '客户名称', grid: 12 / 24 },
  { name: 'RDSJ', required: true, type: 'date', label: '认定时间', grid: 12 / 24 },
  { name: 'TLNO', required: true, type: 'text', label: '电话', grid: 12 / 24 },
  { name: 'ADDR', required: true, type: 'text', label: '地址', grid: 12 / 24 },
  { name: 'POCD', required: true, type: 'text', label: '邮编', grid: 12 / 24 },
  { name: 'EMAL', required: true, type: 'text', label: '电子邮箱', grid: 12 / 24 },
];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
/**对公贷款 */
class BlIndiClieForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false,
      UserData: {}
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID", permID: this.props.permID }

    });
    let sort = {
      field: 'RDSJ',
      order: 'descend'
    }
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 20, sorter: sort }
    });

    this.loadOptionData();
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  saveData() {
    let KHID = this.state.UserData.NO;
    let date = new Date()
    let month = date.getMonth() + 1
    let RDSJ = date.getFullYear() + "-" + (month < 10 ? "0" + month : month) + "-" + "01";
    if (KHID === undefined) {
      message.error("请输入证件号码或者客户姓名")
      return;
    }
    let ISBL = 1
    Modal.confirm({
      title: '提示',
      content: `确定将客户【` + this.state.UserData.Name + `】认定为不良客户吗？`,
      okText: '确定',
      cancelText: '取消',
      onOk: () => {
        accountService.UpIndiClieIsbl({ KHID, RDSJ, ISBL }).then(function (data) {
          if (data === "OK") {
            message.success("零售不良认定成功！")
          }
          let sort = {
            field: 'RDSJ',
            order: 'descend'
          }
          this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20, sorter: sort }
          });

        }.bind(this))
      }
    })

  }
  DeleteData() {
    const len = this.props.selectedRowKeys.length;
    if (len === 0) {
      message.error('请选择要删除的数据行！');
      return;
    }
    const KHID = this.props.selectedRowKeys[0];
    let RDSJ = null
    let ISBL = 0
    Modal.confirm({
      title: '提示',
      content: `确定要删除该客户的不良认定吗？`,
      okText: `确定`,
      cancelText: `取消`,
      onOk: () => {
        accountService.UpIndiClieIsbl({ KHID, RDSJ, ISBL }).then(function (data) {
          if (data === "OK") {
            message.success("零售不良删除成功！")
          }
          let sort = {
            field: 'RDSJ',
            order: 'descend'
          }
          this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20, sorter: sort }
          });
          const form = this.refs.findForm.getForm();
          form.resetFields();
        }.bind(this))
      }
    })

  }
  searchHandle(data) {
    this.state.UserData = data;
  }
  render() {
    let { showEdit, record, permID, popedomTypes } = this.props;
    let pTypes = popedomTypes[permID]
    let data = []
    const controlsFilter = [];
    if (pTypes.indexOf("2") !== -1) {
      data.push({ value: '1', label: '新增不良客户', onClick: this.saveData.bind(this) }, { value: '2', label: '删除不良客户', onClick: this.DeleteData.bind(this) })
      controlsFilter.push({
        name: 'GRKH', required: false, type: 'search', label: '个人客户', grid: 12 / 24, layout: { labelSpan: 8, wrapperSpan: 15 },
        fetch: { daoName: "TIndiClie", searchColumns: ['Name', 'IDNO'], optionTpl: 'Name', valueTpl: "NO", onSelect: this.searchHandle.bind(this), permID: permID },
        placeholder: '请输入证件号码或客户姓名'
      })
      controlsFilter.push({ name: 'CX', required: false, type: 'button-group', data: data, grid: 7 / 24 })
    }
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            <EditFrom ref="findForm" daoName={daoName} controls={controlsFilter} clearItemMargin={true} record={{ qygm: 'all' }} selectFirst={false} />
          </div>
          <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
        </div>
      </div>
    )
  }
}
function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  return {
    userID,
    orgID,
    pOrgID,
    popedomTypes,
    showEdit,
    selectedRowKeys,
    keyName,
    record
  }
}
BlIndiClieForm = connect(mapStateToProps)(BlIndiClieForm)
export default BlIndiClieForm;
let render = (props) => React.createElement(BlIndiClieForm, props);
let f = new Factory()
f.set("BlIndiClieForm", render)